﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyEmulators2
{
    public class EmuSettingsAutoFill
    {
        /// <summary>
        /// Flag used to denote directory where emulator exe is located
        /// </summary>
        public static readonly string USE_EMULATOR_DIRECTORY = "%EMU_EXE_DIR%";

        Dictionary<String, String> filterDictionary;
        object filterSync = new object();

        public EmuSettingsAutoFill()
        {
            lock (filterSync)
            {
                filterDictionary = new Dictionary<String, String>();

                //Add all auto-configurable entries
                filterDictionary.Add("snes9xw.exe", "*.smc");
                filterDictionary.Add("zsnesw.exe", "*.smc;*.fig");
                filterDictionary.Add("project64.exe", "*.n64;*.z64;*.v64;*.rom");
                filterDictionary.Add("fceu.exe", "*.nes");
                filterDictionary.Add("fceux.exe", "*.nes");
                filterDictionary.Add("snes9x.exe", "*.bin;*.fig;*.smc");
                filterDictionary.Add("vba.exe", "*.gba;*.gbc;*.sgb;*.gb");
                filterDictionary.Add("atari800win.exe", "*.atr");
                filterDictionary.Add("fusion.exe", "*.bin;*.smd");
                filterDictionary.Add("nulldc.exe", "*.cdi");
                filterDictionary.Add("epsxe.exe", "*.bin;*.iso;*.img");
                filterDictionary.Add("winuae.exe", "*.uae");
            }
        }

        static EmuSettingsAutoFill instance = null;
        public static EmuSettingsAutoFill Instance
        {
            get
            {
                if (instance == null)
                    instance = new EmuSettingsAutoFill();
                return instance;
            }
        }

        public EmulatorProfile CheckForSettings(string emuPath, out string filters)
        {
            emuPath = getExeName(emuPath);

            EmulatorProfile profile = new EmulatorProfile(null, true); //empty profile
            lock (filterSync)
            {
                if (filterDictionary.ContainsKey(emuPath))
                    filters = filterDictionary[emuPath];
                else
                    filters = null;
            }

            switch (emuPath)
            {
                case "project64.exe":
                    profile.Title = "Project 64";
                    profile.UseQuotes = false;
                    break;
                case "epsxe.exe":
                    profile.Title = "ePSXe";
                    profile.Arguments = "-nogui -loadbin";
                    profile.EscapeToExit = true;
                    break;
                case "mame.exe":
                case "mamep.exe":
                case "mameui32.exe":
                    profile.Title = "Mame";
                    profile.WorkingDirectory = USE_EMULATOR_DIRECTORY;
                    profile.UseQuotes = false;
                    break;
                default:
                    return null;
            }

            return profile;
                
        }

        public string CheckForFilters(string emuPath)
        {
            emuPath = getExeName(emuPath);

            string filter;
            if (filterDictionary.TryGetValue(emuPath, out filter))
                return filter;

            return null;
        }

        string getExeName(string input)
        {
            string ret = input;
            int index = ret.LastIndexOf("\\");
            if (index > -1 && index < ret.Length - 1)
                ret = ret.Substring(index + 1);

            return ret.ToLower();
        }
    }


}
